home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / doc.lha / documentation / manual / number.mss < prev    next >
Text File  |  1987-06-30  |  14KB  |  493 lines

  1. @part[NUMBER, Root "TMAN.MSS"]  @Comment{-*-System:TMAN-*-}
  2. @chap[Numbers]
  3.  
  4.  
  5. @iix[Numbers] in @Tau[] are objects which represent real numbers.
  6. Numbers come in three varieties: integers, ratios, and floating point
  7. numbers.  Integers and ratios are exact models of the corresponding
  8. mathematical objects.  Floating point numbers give discrete approximations
  9. to real numbers within a certain implementation-dependent range.
  10.  
  11. @dc{ Talk about external syntax of numbers. }
  12.  
  13. As expressions, numbers are self-evaluating literals (see section
  14. @ref[EQ?]).
  15.   @begin[ProgramExample]
  16. -377            @ev[]  -377
  17. 72723460248141  @ev[]  72723460248141
  18. 13/5            @ev[]  13/5
  19. 34.55           @ev[]  34.55
  20.   @end[ProgramExample]
  21. @dc{
  22. (define (fib)
  23.   (do ((a 0 b)
  24.        (b 1 (+ a b))
  25.        (n 1 (+ n 1)))
  26.       (nil)
  27.     (format t " (fib ~s) = ~s~%" n b)))
  28.  
  29. (define tau (/ (+ 1 (sqrt 5.0)) 2))
  30.  
  31. (define (fib n)
  32.   (/ (+ (expt tau n) (expt tau (- 0 n))) (sqrt 5.0)))
  33. }
  34.  
  35. There may be many different objects which all represent the same number.
  36. The effect of this is that @tc[EQ?] may behave in nonintuitive ways when
  37. applied to numbers.  It is guaranteed that if two numbers have different
  38. types or different numerical values, then they are @i[not]
  39. @tc[EQ?], but two numbers that appear to be the same may or may not be
  40. @tc[EQ?], even though there is no other way to distinguish them.  Use
  41. @tc[EQUAL?] or @tc[=] (page @PageRef(EQUAL?))  for comparing numbers.
  42.  
  43.  
  44. @section[Predicates]
  45.  
  46. @info[NOTES="Type predicate",EQUIV="NUMBERP"]
  47. @desc[@el[](NUMBER? @i[object]) @yl[] @i[boolean]]
  48. Returns true if @i[object] is a number.
  49. @EndDesc[NUMBER?]
  50.  
  51. @info[NOTES="Type predicate",EQUIV="FIXP"]
  52. @desc[(INTEGER? @i[object]) @yl[] @i[boolean]]
  53. Returns true if @i[object] is an integer.
  54. @EndDesc[INTEGER?]
  55.  
  56. @info[NOTES="Type predicate",EQUIV="FLOATP"]
  57. @desc[(FLOAT? @i[object]) @yl[] @i[boolean]]
  58. Returns true if @i[object] is a floating point number.
  59. @EndDesc[FLOAT?]
  60.  
  61. @info[NOTES="Type predicate"]
  62. @desc[(RATIO? @i[object]) @yl[] @i[boolean]]
  63. Returns true if @i[object] is a ratio.
  64. @EndDesc[RATIO?]
  65.  
  66. @info[EQUIV="ODDP"]
  67. @desc[(ODD? @i[integer]) @yl[] @i[boolean]]
  68. Returns true if @i[integer] is odd.
  69. @EndDesc[ODD?]
  70.  
  71. @desc[(EVEN? @i[integer]) @yl[] @i[boolean]]
  72. Returns true if @i[integer] is even.
  73. @EndDesc[EVEN?]
  74.  
  75.  
  76. @begin[group]
  77. @section[Arithmetic]
  78.  
  79. @info[EQUIV="PLUS"]
  80. @descN[
  81. F1="@el[](+ . @i[numbers]) @yl[] @i[number]", FN1="+", NL1,
  82. F2="(ADD . @i[numbers]) @yl[] @i[number]",    FN2="ADD"
  83. ]
  84. Returns the sum of the @i[numbers].
  85. @begin[ProgramExample]
  86. (+ 10 27)    @ev[]  37
  87. (+ 10 27.8)  @ev[]  37.8
  88. (+)          @ev[]  0
  89. (+ -35)      @ev[]  -35
  90. (+ 1 2 3)    @ev[]  6
  91. @end[ProgramExample]
  92. @EndDescN[]
  93. @end[group]
  94.  
  95. @info[EQUIV="DIFFERENCE"]
  96. @descN[
  97. F1="@el[](- @i[n1 n2]) @yl[] @i[number]",   FN1="-"   ,
  98. F2="(SUBTRACT @i[n1 n2]) @yl[] @i[number]", FN2="SUBTRACT"
  99. ]
  100. Returns the difference of @i[n1] and @i[n2].
  101. @EndDescN[]
  102.  
  103. @info[EQUIV="MINUS"]
  104. @descN[
  105. F1="@el[](- @i[n]) @yl[] @i[number]",
  106. F2="(NEGATE @i[n]) @yl[] @i[number]",  FN2="NEGATE"
  107. ]
  108. Returns the additive inverse of @i[n].
  109. @EndDescN[]
  110.  
  111. @info[EQUIV="TIMES"]
  112. @descN[
  113. F1="@el[](* . @i[numbers]) @yl[] @i[number]",   FN1="*" ,
  114. F2="(MULTIPLY . @i[numbers]) @yl[] @i[number]", FN2="MULTIPLY"
  115. ]
  116. Returns the product of the @i[numbers].
  117. @EndDescN[]
  118.  
  119. @info[EQUIV="QUOTIENT"]
  120. @descN[
  121. F1="@el[](/ @i[n1 n2]) @yl[] @i[number]", FN1="/", NL1,
  122. F2="(DIVIDE @i[n1 n2]) @yl[] @i[number]", FN2="DIVIDE"
  123. ]
  124. Returns the quotient of @i[n1] and @i[n2].
  125. @begin[ProgramExample]
  126. (/ 20 5)    @ev[]  4
  127. (/ 5 20)    @ev[]  1/4
  128. (/ 5.0 20)  @ev[]  0.25
  129. @end[ProgramExample]
  130. @EndDescN[]
  131.  
  132. @info[EQUIV="//"]
  133. @desc[(DIV @i[n1 n2]) @yl[] @i[integer]]
  134. Integer division; truncates the quotient of @i[n1] and @i[n2]
  135. towards zero.
  136.     @BeginInset[Bug:]
  137.     @tc[DIV] in @Timp[] 2.7 does strange things if its arguments
  138.     aren't integers.
  139.     @EndInset[]
  140. @EndDesc[DIV]
  141.  
  142. @info[EQUIV="REMAINDER, \"]
  143. @desc[(REMAINDER @i[n1 n2]) @yl[] @i[number]]
  144. Returns the remainder of the division of @i[n1] by @i[n2], with
  145. the sign of @i[n1].
  146.     @BeginInset[Bug:]
  147.     @tc[REMAINDER] in @Timp[] 2.7 does strange things
  148.     if its arguments aren't integers.
  149.     @EndInset[]
  150. @EndDesc[REMAINDER]
  151.  
  152. @dc{
  153. @desc[(FLOOR @i[n1 n2]) @yl[] @i[number]]
  154. Returns the greatest multiple of @i[n2] which is less than or equal to @i[n1];
  155. @qu"round down."
  156. A particularly useful case is where @i[n2] is 1, in which case
  157. @tc[FLOOR] returns @i[n1]'s integer part.
  158.   @begin[ProgramExample]
  159. (FLOOR 21 8)    @ev[]  16
  160. (FLOOR -21 8)   @ev[]  -24
  161. (FLOOR 2.67 1)  @ev[]  2
  162.   @end[ProgramExample]
  163. @EndDesc[FLOOR]
  164.  
  165. @desc[(CEILING @i[n1 n2]) @yl[] @i[number]]
  166. Returns the least multiple of @i[n2] which is greater than or equal to @i[n1];
  167. @qu"round up."
  168.   @begin[ProgramExample]
  169. (CEILING 21 8)    @ev[]  24
  170. (CEILING -21 8)   @ev[]  16
  171. (CEILING 2.67 1)  @ev[]  3
  172.   @end[ProgramExample]
  173. @EndDesc[CEILING]
  174.  
  175. @desc[(TRUNCATE @i[n1 n2]) @yl[] @i[number]]
  176. Returns the greatest multiple of @i[n2] which is less than or equal to the
  177. absolute value of @i[n1], with @i[n1]'s sign;
  178. @qu"round towards zero."
  179.   @begin[ProgramExample]
  180. (TRUNCATE 21 8)    @ev[]  16
  181. (TRUNCATE -21 8)   @ev[]  -24
  182. (TRUNCATE 2.67 1)  @ev[]  1
  183.   @end[ProgramExample]
  184. @EndDesc[TRUNCATE]
  185.  
  186. @desc[(ROUND @i[n1 n2]) @yl[] @i[number]]
  187. Rounds @i[n1] toward the nearest multiple of @i[n2].
  188. @EndDesc[ROUND]
  189. }
  190.  
  191. @desc[(MOD @i[n1 n2]) @yl[] @i[number]]
  192. Returns a number @i[k] in the range from zero, inclusive, to @i[n2],
  193. exclusive, such that @i[k] differs from @i[n1] by an integral
  194. multiple of @i[n2].  If @i[n1] is positive, this behaves like
  195. @tc[REMAINDER].
  196.     @BeginInset[Bug:]
  197.     @tc[MOD] in @Timp[] 2.7 does strange things if its arguments aren't integers.
  198.     @EndInset[]
  199. @EndDesc[MOD]
  200.  
  201. @desc[(EXPT @i[base exponent]) @yl[] @i[number]]
  202. Returns @i[base] raised to the @i[exponent] power.
  203. @i[Base] and @i[exponent] may be any numbers.
  204.     @BeginInset[Bug:]
  205.     In @Timp[] 2.7, @i[exponent] must be an integer.
  206.     @EndInset[]
  207. @EndDesc[EXPT]
  208.  
  209. @desc[(ABS @i[n]) @yl[] @i[number]]
  210. Returns the absolute value of @i[n].
  211. @EndDesc[ABS]
  212.  
  213. @desc[(GCD @i[integer1 integer2]) @yl[] @i[integer]]
  214. Returns the greatest common divisor of @i[integer1] and @i[integer2].
  215.   @begin[ProgramExample]
  216. (GCD 36 60)  @ev[]  12
  217.   @end[ProgramExample]
  218. @EndDesc[GCD]
  219.  
  220. @AnEquivE[Tfn="ADD1",Efn="1+"]
  221. @AnEquivE[Tfn="ADD1",Efn="ADD1"]
  222. @descN[
  223. F1="(ADD1 @i[n]) @yl[] @i[number]", FN1="ADD1",
  224. F2="(1+ @i[n]) @yl[] @i[number]", FN2="1+", NL2
  225. ]
  226. Returns @i[n] + 1.
  227. (See also @tc[INCREMENT], page @PageRef[INCREMENT].)
  228. @EndDescN[]
  229.  
  230. @AnEquivE[Tfn="SUBTRACT1",Efn="1-"]
  231. @AnEquivE[Tfn="SUBTRACT1",Efn="SUB1"]
  232. @descN[
  233. F1="(SUBTRACT1 @i[n]) @yl[] @i[number]", FN1="SUBTRACT1",
  234. F2="(-1+ @i[n]) @yl[] @i[number]", FN2="-1+", NL2
  235. ]
  236. Returns @i[n] - 1.
  237. (See also @tc[DECREMENT], page @PageRef[DECREMENT].)
  238. @EndDescN[]
  239.  
  240. @desc[(MIN . @i[numbers]) @yl[] @i[number]]
  241. Returns the @i[number] with the least numerical magnitude.
  242. (There must be at least one @i[number].)
  243. @EndDesc[MIN]
  244.  
  245. @desc[(MAX . @i[numbers]) @yl[] @i[number]]
  246. Returns the @i[number] with the greatest numerical magnitude.
  247. (There must be at least one @i[number].)
  248. @EndDesc[MAX]
  249.  
  250.  
  251. @section[Comparison]
  252.  
  253. @descN[
  254. F1="@el[](= @i[n1 n2]) @yl[] @i[boolean]", FN1="=", NL1,
  255. F2="(EQUAL? @i[n1 n2]) @yl[] @i[boolean]", FN2="EQUAL?"
  256. ]
  257. @label[EQUAL]
  258. Returns true if @i[n1] is numerically equal to @i[n2].
  259. @EndDescN[]
  260.  
  261. @AnEquivE[Tfn="LESS?",Efn="LESSP"]
  262. @descN[
  263. F1="@el[](< @i[n1 n2]) @yl[] @i[boolean]", FN1="<", NL1,
  264. F2="(LESS? @i[n1 n2]) @yl[] @i[boolean]",  FN2="LESS?"
  265. ]
  266. Returns true if @i[n1] is numerically less than @i[n2].
  267. @EndDescN[]
  268.  
  269. @AnEquivE[Tfn="GREATER?",Efn="GREATERP"]
  270. @descN[
  271. F1="@el[](> @i[n1 n2]) @yl[] @i[boolean]",   FN1=">", NL1,
  272. F2="(GREATER? @i[n1 n2]) @yl[] @i[boolean]", FN2="GREATER?"
  273. ]
  274. Returns true if @i[n1] is numerically greater than @i[n2].
  275. @EndDescN[]
  276.  
  277. @descN[
  278. F1="(N= @i[n1 n2]) @yl[] @i[boolean]",         FN1="N=",         NL1,
  279. F2="(NOT-EQUAL? @i[n1 n2]) @yl[] @i[boolean]", FN2="NOT-EQUAL?"
  280. ]
  281. Returns true if @i[n1] is not numerically equal to @i[n2].
  282. @EndDescN[]
  283.  
  284. @descN[
  285. F1="(>= @i[n1 n2]) @yl[] @i[boolean]",        FN1=">=", NL1,
  286. F2="(NOT-LESS? @i[n1 n2]) @yl[] @i[boolean]", FN2="NOT-LESS?"
  287. ]
  288. Returns true if @i[n1] is not numerically less than @i[n2].
  289. @EndDescN[]
  290.  
  291. @descN[
  292. F1="(<= @i[n1 n2]) @yl[] @i[boolean]",           FN1="<=", NL1,
  293. F2="(NOT-GREATER? @i[n1 n2]) @yl[] @i[boolean]", FN2="NOT-GREATER?"
  294. ]
  295. Returns true if @i[n1] is not numerically greater than @i[n2].
  296. @EndDescN[]
  297.  
  298. @section[Sign predicates]
  299.  
  300. @AnEquivE[Tfn="ZERO?",Efn="ZEROP"]
  301. @descN[
  302. F2="(=0? @i[n]) @yl[] @i[boolean]",    FN2="=0?", NL2,
  303. F1="(ZERO? @i[n]) @yl[] @i[boolean]",  FN1="ZERO?"
  304. ]
  305. Returns true if @i[n] is numerically equal to zero.
  306. @EndDescN[]
  307.  
  308. @AnEquivE[Tfn="NEGATIVE?",Efn="MINUSP"]
  309. @descN[
  310. F2="(<0? @i[n]) @yl[] @i[boolean]",        FN2="<0?", NL2,
  311. F1="(NEGATIVE? @i[n]) @yl[] @i[boolean]",  FN1="NEGATIVE?"
  312. ]
  313. Returns true if @i[n] is negative.
  314. @EndDescN[]
  315.  
  316. @AnEquivE[Tfn="POSITIVE?",Efn="PLUSP"]
  317. @descN[
  318. F2="(>0? @i[n]) @yl[] @i[boolean]",        FN2=">0?", NL2,
  319. F1="(POSITIVE? @i[n]) @yl[] @i[boolean]",  FN1="POSITIVE?"
  320. ]
  321. Returns true if @i[n] is positive.
  322. @EndDescN[]
  323.  
  324. @descN[
  325. F2="(N=0? @i[n]) @yl[] @i[boolean]",    FN2="N=0?",         NL2,
  326. F1="(NOT-ZERO? @i[n]) @yl[] @i[boolean]",  FN1="NOT-ZERO?"
  327. ]
  328. Returns true if @i[n] is not numerically equal to zero.
  329. @EndDescN[]
  330.  
  331. @descN[
  332. F2="(>=0? @i[n]) @yl[] @i[boolean]",        FN2=">=0?", NL2,
  333. F1="(NOT-NEGATIVE? @i[n]) @yl[] @i[boolean]",  FN1="NOT-NEGATIVE?"
  334. ]
  335. Returns true if @i[n] is non-negative.
  336. @EndDescN[]
  337.  
  338. @descN[
  339. F2="(<=0? @i[n]) @yl[] @i[boolean]",        FN2="<=0?", NL2,
  340. F1="(NOT-POSITIVE? @i[n]) @yl[] @i[boolean]",  FN1="NOT-POSITIVE?"
  341. ]
  342. Returns true if @i[n] is non-positive.
  343. @EndDescN[]
  344.  
  345. @section[Transcendental functions]
  346.  
  347. @desc[(EXP @i[float]) @yl[] @i[float]]
  348. Exponential function (e@+[@i[x]]).
  349. @EndDesc[EXP]
  350.  
  351. @desc[(LOG @i[float]) @yl[] @i[float]]
  352. Natural logarithm (log@-[e]@i[x]).
  353. @EndDesc[LOG]
  354.  
  355. @desc[(SQRT @i[float]) @yl[] @i[float]]
  356. Returns the square root of its argument.
  357. @EndDesc[SQRT]
  358.  
  359. @desc[(COS @i[float]) @yl[] @i[float]]
  360. Returns the cosine of its argument.
  361. @EndDesc[COS]
  362.  
  363. @desc[(SIN @i[float]) @yl[] @i[float]]
  364. Returns the sine of its argument.
  365. @EndDesc[SIN]
  366.  
  367. @desc[(TAN @i[float]) @yl[] @i[float]]
  368. Returns the tangent of its argument.
  369. @EndDesc[TAN]
  370.  
  371. @desc[(ACOS @i[float1]) @yl[] @i[float2]]
  372. Arccosine.  Returns a number whose cosine is @i[float1].
  373. @EndDesc[ACOS]
  374.  
  375. @desc[(ASIN @i[float1]) @yl[] @i[float2]]
  376. Arcsine.  Returns a number whose sine is @i[float1].
  377. @EndDesc[ASIN]
  378.  
  379. @desc[(ATAN2 @i[x y]) @yl[] @i[float]]
  380. Two-argument arctangent.  This computes the
  381. angle between the positive x-axis and the point
  382. (@i[x], @i[y]).  For example, if (@i[x], @i[y]) is in the first or third
  383. quadrant, @tc[(ATAN2 @i[x y])] returns the arctangent of @i[y]/@i[x].
  384.     @BeginInset[Bug:]
  385.     @tc[ATAN2] isn't implemented in @Timp[] 2.7.  @Timp[] 2.7 does
  386.     implement @tc[ATAN] (arctangent), however.
  387.     @EndInset[]
  388. @EndDesc[ATAN2]
  389.  
  390. @section[Bitwise logical operators]
  391.     @BeginInset[Bug:]
  392.     In @Timp[] 2.7, the routines in this section are restricted to take
  393.     fixnums (see page @pageref[FIXNUM?]), not arbitrary integers.
  394.     @EndInset[]
  395.  
  396. @desc[(LOGAND @i[integer1 integer2]) @yl[] @i[integer]]
  397. Returns the bitwise logical @i[and] of its arguments.
  398.   @begin[ProgramExample]
  399. (LOGAND 10 12)  @ev[]  8
  400.   @end[ProgramExample]
  401. @EndDesc[LOGAND]
  402.  
  403. @desc[(LOGIOR @i[integer1 integer2]) @yl[] @i[integer]]
  404. Returns the bitwise logical inclusive @i[or] of its arguments.
  405. @EndDesc[LOGIOR]
  406.  
  407. @desc[(LOGXOR @i[integer1 integer2]) @yl[] @i[integer]]
  408. Returns the bitwise logical exclusive @i[or] of its arguments.
  409. @EndDesc[LOGXOR]
  410.  
  411. @desc[(LOGNOT @i[integer]) @yl[] @i[integer]]
  412. Returns the bitwise logical @i[not] of its argument.
  413. @EndDesc[LOGNOT]
  414.  
  415. @desc[(ASH @i[integer amount]) @yl[] @i[integer]]
  416. Shifts @i[integer] to the left @i[amount] bit positions.
  417. If @i[amount] is negative, shifts @i[integer] right by the absolute
  418. value of @i[amount] bit positions.
  419. @EndDesc[ASH]
  420.  
  421. @desc[(BIT-FIELD @i[integer position size]) @yl[] @i[integer]]
  422. Extracts a bit field out of @i[integer].  The field extracted begins
  423. at bit position @i[position] from the low-order bit of the integer
  424. and consists of @i[size] bits.  The extracted field is returned
  425. as a positive integer.
  426.   @begin[ProgramExample]
  427. (BIT-FIELD 27 1 3)  @ev[]  5
  428. (BIT-FIELD -1 4 5)  @ev[]  31
  429.   @end[ProgramExample]
  430. @EndDesc[BIT-FIELD]
  431.  
  432. @desc[(SET-BIT-FIELD @i[integer position size value]) @yl[] @i[integer]]
  433. Inserts a value into a bit field of @i[integer].  The field begins
  434. at bit position @i[position] from the low-order bit of the integer
  435. and consists of @i[size] bits.  A new integer is returned which is the same as the
  436. argument @i[integer] except that this field has been altered to be @i[value].
  437.   @begin[ProgramExample]
  438. (SET-BIT-FIELD 32 1 3 5)  @ev[]  42
  439.   @end[ProgramExample]
  440. @EndDesc[SET-BIT-FIELD]
  441.  
  442. @section[Coercion]
  443.  
  444. @desc[(->INTEGER @i[number]) @yl[] @i[integer]]
  445. Coerces @i[number] to an integer, truncating towards zero if necessary.
  446.   @begin[ProgramExample]
  447. (->INTEGER 17)     @ev[]  17
  448. (->INTEGER 7/4)    @ev[]  1
  449. (->INTEGER 17.6)   @ev[]  17
  450. (->INTEGER -17.6)  @ev[]  -17
  451.   @end[ProgramExample]
  452. @enddesc[->INTEGER]
  453.  
  454. @desc[(->FLOAT @i[number]) @yl[] @i[float]]
  455. Coerces @i[number] to a floating point number.
  456.   @begin[ProgramExample]
  457. (->FLOAT 17)    @ev[]  17.0
  458. (->FLOAT 7/4)   @ev[]  1.75
  459. (->FLOAT 17.6)  @ev[]  17.6
  460.   @end[ProgramExample]
  461. @enddesc[->FLOAT]
  462.  
  463. @section[Assignment]
  464.  
  465. @tc[INCREMENT] and @tc[DECREMENT] are assignment forms, like @tc[SET]
  466. and @tc[PUSH].  See section @ref[Assignmentsection] for a general
  467. discussion.
  468.  
  469. @info[Notes "Special form"]
  470. @desc[(INCREMENT @i[location]) @yl[] @i[number]]
  471. Adds one to the value in @i[location], stores the sum back into @i[location],
  472. and yields the sum.
  473.   @begin[ProgramExample]
  474. (LSET L 6)
  475. (INCREMENT L)             @ev[]  7
  476. L                         @ev[]  7
  477. (LSET M (LIST 10 20 30))  @ev[]  (10 20 30)
  478. (INCREMENT (CAR M))       @ev[]  11
  479. M                         @ev[]  (11 20 30)
  480.  
  481. (INCREMENT @i[location])  @ce[]  (MODIFY @i[location] ADD1)
  482.   @end[ProgramExample]
  483. @EndDesc[INCREMENT]
  484.  
  485. @info[Notes "Special form"]
  486. @desc[(DECREMENT @i[location]) @yl[] @i[number]]
  487. Subtracts one from the value in @i[location], stores the
  488. difference back into @i[location], and yields the difference.
  489.   @begin[ProgramExample]
  490. (DECREMENT @i[location])  @ce[]  (MODIFY @i[location] SUBTRACT1)
  491.   @end[ProgramExample]
  492. @EndDesc[DECREMENT]
  493.